//
//  TestNetTool.m
//  POP_User
//
//  Created by Rowling on 2019/10/24.
//  Copyright © 2019 Rowling. All rights reserved.
//

#import "TestNetTool.h"

static TestNetTool *tool = nil;
static dispatch_once_t onceToken;

@implementation TestNetTool
//singleton_implementation(TestNetTool);

+ (TestNetTool *)shareInstance{
    dispatch_once(&onceToken, ^{
        tool = [[TestNetTool alloc] init];;
        tool.isOpen = YES;
    });
    return tool;
}

-(TestView *)testView {
    if (_testView == nil) {
        _testView = [TestView fastLogin];
        _testView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

    }
    return _testView;
}

-(NSMutableArray<TestNetToolModel *> *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}

-(NSMutableArray<SocketModel *> *)socektDataArray {
    if (_socektDataArray == nil) {
        _socektDataArray = [[NSMutableArray alloc]init];
    }
    return _socektDataArray;
}

-(TestButton *)testBtn {
    if (_testBtn == nil) {
        _testBtn = [[TestButton alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height / 2, 50, 50)];
        _testBtn.layer.shadowOpacity = 0.5;
        _testBtn.layer.shadowOffset = CGSizeMake(5, 5);
//        _testBtn.clipsToBounds = YES;
//        _testBtn.backgroundColor = [UIColor yellowColor];
    }
    return _testBtn;
}

-(void)addRecordUrl:(NSString *)url param:(id)param method:(NSString *)method respons:(id)respons {
    [self addRecordUrl:url header:nil param:param method:method respons:respons];
}

-(void)addRecordUrl:(NSString *)url header:(nullable id)header param:(id)param method:(NSString *)method respons:(id)respons {
    if (!self.isOpen)return;
    TestNetToolModel * model = [TestNetToolModel new];
    model.urlStr = url;
    if (param) {
        model.paramStr = [[param description] stringByReplacingOccurrencesOfString:@";" withString:@","];
    }
    if (header) {
        model.headerStr = [[header description] stringByReplacingOccurrencesOfString:@";" withString:@","];
    }
    model.methodStr = method;
    if (respons) {
        model.responsStr = [respons description];
    }
    NSDateFormatter * fo = [NSDateFormatter new];
    fo.dateFormat = @"HH:mm:ss";
    NSString * tiemStr = [fo stringFromDate:[NSDate date]];
    model.timeStr = tiemStr;
    [self.dataArray insertObject:model atIndex:0];
    NSString * desStr;
    if (header) {
         desStr = [NSString stringWithFormat:@"时间:%@--%@\nHttp请求\n请求地址-->%@\n请求头-->%@\n请求参数:-->\n%@\n返回值:-->%@\n",model.timeStr,model.methodStr,model.urlStr,model.headerStr,model.paramStr,model.responsStr];
    }
    else {
         desStr = [NSString stringWithFormat:@"时间:%@--%@\nHttp请求\n请求地址-->%@\n请求参数:-->\n%@\n返回值:-->%@\n",model.timeStr,model.methodStr,model.urlStr,model.paramStr,model.responsStr];
    }
    [NSNotificationCenter.defaultCenter postNotificationName:@"TestAddSomething" object:desStr];
    [model caculate];
//    [self.testView reloadData];
}




-(void)addSocketModel:(NSDictionary *)dic {
    if (!self.isOpen)return;
    SocketModel * model = [SocketModel new];
    model.dic = [dic description];
    model.rawDic = dic;
    NSDateFormatter * fo = [NSDateFormatter new];
    fo.dateFormat = @"HH:mm:ss";
    NSString * tiemStr = [fo stringFromDate:[NSDate date]];
    model.timeStr = tiemStr;
    [self.socektDataArray insertObject:model atIndex:0];
//    [self.testView reloadData];
    
    NSString * desStr = [NSString stringWithFormat:@"时间:%@\nSocket消息\n收到的数据%@",model.timeStr,model.dic];
    [NSNotificationCenter.defaultCenter postNotificationName:@"TestAddSomething" object:desStr];
}

//-(NSString *)baseUrl {
//    if (_baseUrl == nil) {
//        _baseUrl = [self urlInfo][0];
//    }
//    return _baseUrl;
//}
//
//-(NSString *)socketUrl {
//    if (_socketUrl == nil) {
//        _socketUrl = [self urlInfo][1];
//    }
//    return _socketUrl;
//}
//
//-(NSNumber *)socketPort {
//    if (_socketPort == nil) {
//        _socketPort = [self urlInfo][2];
//    }
//    return _socketPort;
//}

//-(NSArray *)urlInfo {
//    NSDictionary * dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"urlSetting"];
//    if (dic) {
//        return @[[NSString stringWithFormat:@"https://%@/rest",dic[@"Base_Url"]],dic[@"Socket_Url"],dic[@"Socket_Port"]];
//    } else {
//        return @[Base_Url,Socket_Url,@(Socket_Port)];
//    }
//}

@end

@interface TestNetToolModel()

@end

@implementation TestNetToolModel

-(NSAttributedString *)attributesString {
    if (!_attributesString){
        NSString * desStr;
        if (self.headerStr) {
            desStr = [NSString stringWithFormat:@"时间:%@--%@\nHttp请求\n请求地址-->%@\n请求头-->%@\n请求参数:-->\n%@\n返回值:-->%@\n",self.timeStr,self.methodStr,self.urlStr,self.headerStr,self.paramStr,self.responsStr];
        } else {
            desStr = [NSString stringWithFormat:@"时间:%@--%@\n请求地址-->%@\n请求参数:-->\n%@\n返回值:-->%@\n",self.timeStr,self.methodStr,self.urlStr,self.paramStr,self.responsStr];
        }
        
        
        NSString * redStr = [NSString stringWithFormat:@"%@",self.urlStr];
        NSRange range = [desStr rangeOfString:redStr];
        
        NSMutableAttributedString * attriStr = [[NSMutableAttributedString alloc] initWithString:desStr];
        
        [attriStr addAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} range:range];
        
        [attriStr addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16]} range:NSMakeRange(0, attriStr.length)];
        if (self.headerStr) {
            NSString * header = [NSString stringWithFormat:@"%@",self.headerStr];
            NSRange range = [desStr rangeOfString:header];
            [attriStr addAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:0 / 255.0 green:208/255.0 blue:99/255.0 alpha:1]} range:range];
        }
        _attributesString = attriStr;
    }
    return  _attributesString;
}

//-(CGFloat)height {
//    if (_height == 0) {
////        [self.attributesString ]
////        _height = attributesString;
////        NSAttributedString *text = [[EaseEmotionEscape sharedInstance] attStringFromTextForChatting:model.text textFont:cell.messageTextFont];
////        CGRect rect = [text boundingRectWithSize:CGSizeMake(bubbleMaxWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil];
////        height += (rect.size.height > 20 ? rect.size.height : 20) + 10;
////        CGFloat sw = [UIScreen mainScreen].bounds.size.width;
////        CGRect rect = [self.attributesString boundingRectWithSize:CGSizeMake(sw - 30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil];
////        _height = rect.size.height + 10
//    }
//    return  _height;
//}

-(void)caculate {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        CGFloat sw = [UIScreen mainScreen].bounds.size.width;
        CGRect rect = [self.attributesString boundingRectWithSize:CGSizeMake(sw - 30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil];
        self.height = rect.size.height + 10;
    });
}

@end

@implementation SocketModel
@end

